package com.rcl.recyclerviewdemo.presenter;

import com.rcl.recyclerviewdemo.bean.ContentBean;
import com.rcl.recyclerviewdemo.interfaces.IContentPresenter;
import com.rcl.recyclerviewdemo.interfaces.IContentViewCallback;

import java.util.ArrayList;
import java.util.List;

/*
 * class title:
 * Author by RenChunLin, Email 18957806320@163.com, Date on 2020/11/3.
 * PS: Not easy to write code, please indicate.
 */
public class ContentPresenter implements IContentPresenter {

    private static ContentPresenter mContentPresenter;
    private List<IContentViewCallback> mIContentViewCallback = new ArrayList<>();

    private ContentPresenter() {
    }

    public synchronized static ContentPresenter getInstance() {
        if (mContentPresenter == null) {
            mContentPresenter = new ContentPresenter();
        }
        return mContentPresenter;
    }

    @Override
    public void getContentData() {
        List<ContentBean> contentBean = new ArrayList<>();
        for (int i = 1; i < 21; i++) {
            ContentBean content = new ContentBean();
            content.setContent("item" + i);
            contentBean.add(content);
        }
        resultData(contentBean);
    }

    private void resultData(List<ContentBean> contentBean) {
        for (IContentViewCallback iContentViewCallback : mIContentViewCallback) {
            iContentViewCallback.setContentData(contentBean);
        }
    }

    @Override
    public void registeredActivity(IContentViewCallback callback) {
        if (callback != null) {
            if (!mIContentViewCallback.contains(callback)) {
                mIContentViewCallback.add(callback);
            }
        }
    }

    @Override
    public void unregisteredActivity(IContentViewCallback callback) {
        if (callback != null) {
            mIContentViewCallback.remove(callback);
        }
    }
}
